// 选择ztree
var setting = {
  check: {
    enable: true
  },
  data: {
    simpleData: {
      enable: false
    }
  },
  view: {
    showIcon: false
	},
	callback: {
		onCheck: zTreeOnCheck
	}
};

/**标准数据格式*/
var zNodes = [];

// 选中的zTree值
var userIds = []; // 人员id集合
var teamIds = []; // 部门id集合

$(function() {
  $('#attendanceQueryList').getList({
    "initData": {
      "num": 1,
      "size": 10
    },
    "url": site.apiUrl + "/back/attendanceManagement/getAttanceRecordList", // 数据请求url
    "type": "post",
    "ajaxform": true,
    // "checkname": "datachk", //选择器名称，为空不显示选择器
    "col": [ //数据列
      { "field": "nickName", "display": "姓名", "width": "1.5" },
      { "field": "teamName", "display": "部门", "width": "1.5" },
      { "field": "dateOfAttendance", "display": "考勤日期", "width": "1.5" },
      { "field": "inTime", "display": "上班-打卡时间", "width": "1.5" },
      { "field": "inStateVal", "display": "上班-打卡结果", "width": "1.5" },
      { "field": "outTime", "display": "下班-打卡时间", "width": "1.5" },
      { "field": "outStateVal", "display": "下班-打卡结果", "width": "1.5" },
      { "field": "finalStatusVal", "display": "考勤状态" },
      { "field": "attendanceName", "display": "考勤点", "width": "1.5" },
    ],
  });

  // 获取考勤状态字典
  $("#attendanceStatus").getSelect({
    type: "select",
    label: "考勤状态：",
    asName: "attendanceStatus",
    code: "attendanceStatus",
  });

  $("#choosePerson").val("");

  getTeamUser(); // 获取部门与人员集合

  // 点击显示--选择部门人员弹窗
  $("#choosePerson").on("focus", function() {
    layer.open({
      title: false,
      type: 1,
      area: ['600px'], //设置宽度,高度自适应
      closeBtn: 0,
      content: $(".layerBox"),
      success: function(layero, index) {
        $("#layui-layer" + index).find(".goback, .icon-close").on("click", function() {
          $("#choosePerson").val("");
          $("#userIds").val("");
          $("#teamIds").val("");
          $("#ztreeCheckedNames").html("");
          $.fn.zTree.init($("#treeDemo"), setting, zNodes); // 加载ztree
          var zTreeObj = $.fn.zTree.getZTreeObj("treeDemo"); // ztree的Id
          zTreeObj.checkAllNodes(false); // 清除所有节点选中状态
          zTreeObj.expandAll(false); // 收起所有节点
					layer.close(index);
        });
        $("#saveBox-btn").on("click", function() {
					getzTreeChecked();
					layer.close(index);
        });
      }
    });
  })

  // 起止日期选择
  laydate.render({
    elem: '#startEndDate',
    theme: 'molv',
    range: '~',
    max: 0,
    trigger: 'click',
    done: function(value, date, endDate){
      if (value != '') {
        var startTime = value.split(" ~ ")[0];
        var endTime = value.split(" ~ ")[1];
        $("#startDate").val(startTime);
        $("#endDate").val(endTime);
      }
    }
  });

  // 点击清空按钮
  $("#reset").on("click", function() {
    $("#startDate").val("");
    $("#endDate").val("");
    $("#choosePerson").val("");
    $("#userIds").val("");
    $("#teamIds").val("");

    $("#ztreeCheckedNames").html("");
    $.fn.zTree.init($("#treeDemo"), setting, zNodes); // 加载ztree
    var zTreeObj = $.fn.zTree.getZTreeObj("treeDemo"); // ztree的Id
    zTreeObj.checkAllNodes(false); // 清除所有节点选中状态
    zTreeObj.expandAll(false); // 收起所有节点
  })

});

// zTree发生改变
function zTreeOnCheck(event, treeId, treeNode) {
	var zTreeObj = $.fn.zTree.getZTreeObj("treeDemo"); // ztree的Id
  var checkedNodes = zTreeObj.getCheckedNodes();
  $("#ztreeCheckedNames").html('');
  var choosePersonVal = '';
	for(var i = 0; i < checkedNodes.length; i++) {
    if (checkedNodes[i].children) {
      $("#ztreeCheckedNames").append('<li class="parent_li">'+ checkedNodes[i].name +'</li>')
    } else {
      $("#ztreeCheckedNames").append('<li class="child_li">'+ checkedNodes[i].name +'</li>')
    }
    choosePersonVal += checkedNodes[i].name + '，';
  }
  choosePersonVal = choosePersonVal.substr(0, choosePersonVal.length - 1);
  $("#choosePerson").val(choosePersonVal);
};

// 获取zTree选中的值
function getzTreeChecked() {
  userIds = []; // 人员id集合
  teamIds = []; // 部门id集合
  var zTreeObj = $.fn.zTree.getZTreeObj("treeDemo"); // ztree的Id
	var checkedNodes = zTreeObj.getCheckedNodes();
	$.each(checkedNodes, function(i, o) {
		if (checkedNodes[i].userId) {
			userIds.push(checkedNodes[i].userId);
		} else if (checkedNodes[i].teamId) {
			teamIds.push(checkedNodes[i].teamId);
		}
  })
  $("#userIds").val(userIds.toString());
  $("#teamIds").val(teamIds.toString());
}

// 获取部门与人员集合
function getTeamUser() {
	request({
    url: site.apiUrl + "/back/team/selectTeamUserByGroupsBack",
    type: "get",
    callback: function(f) {
      if (f.code == "rest.success") {
        zNodes = f.result;
        $("#ztreeCheckedNames").html('');
        $.fn.zTree.init($("#treeDemo"), setting, zNodes); // 加载ztree
        var zTreeObj = $.fn.zTree.getZTreeObj("treeDemo"); // ztree的Id
        zTreeObj.checkAllNodes(false); // 清除所有节点选中状态
        zTreeObj.expandAll(false); // 收起所有节点
      }
    }
  });
}
